﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRSystem.Entity;
using MySql.Data.MySqlClient;
using HRSystem.Utility;

namespace HRSystem.DBAccess
{
    public class ResumeAccess : Access
    {
        public List<Resume> GetResumes(int baseInfoID, int type)
        {
            string sql = "select * from resume where person_id=" + baseInfoID + " and " + "type=" + type;

            List<Resume> list = new List<Resume>();
            using (MySqlDataReader reader = MySqlUtil.GetDataReader(sql, null))
            {
                while (reader.Read())
                {
                    Resume resume = new Resume();
                    resume.ID = CheckIntNull(reader["id"]);
                    resume.EducationSchoolName = CheckNull(reader["education_school_name"]);
                    resume.EducationSchoolCharacter = CheckNull(reader["education_school_character"]);
                    resume.EducationSpecialty = CheckNull(reader["education_specialty"]);
                    resume.EducationdDgree = CheckNull(reader["education_dgree"]);
                    resume.EducationStudyStyle = CheckNull(reader["education_study_style"]);
                    resume.EducationStartTime = CheckDatetimeNull(reader["education_start_time"]);
                    resume.EducationEndTime = CheckDatetimeNull(reader["education_end_time"]);
                    resume.WorkCompany = CheckNull(reader["work_company"]);
                    resume.WorkPosition = CheckNull(reader["work_position"]);
                    resume.WorkRevenue = CheckNull(reader["work_revenue"]);
                    resume.WorkDepartureReason = CheckNull(reader["work_departure_reason"]);
                    resume.WorkProvePersonAndPosition = CheckNull(reader["work_prove_person_and_position"]);
                    resume.WorkProvePersonContact = CheckNull(reader["work_prove_person_contact"]);
                    resume.WorkStartTime = CheckDatetimeNull(reader["work_start_time"]);
                    resume.WorkEndTime = CheckDatetimeNull(reader["work_end_time"]);
                    resume.ProjectName = CheckNull(reader["project_name"]);
                    resume.ProjectIntro = CheckNull(reader["project_intro"]);
                    resume.ProjectDetail = CheckNull(reader["project_detail"]);
                    resume.ProjectStartTime = CheckDatetimeNull(reader["project_start_time"]);
                    resume.ProjectEndTime = CheckDatetimeNull(reader["project_end_time"]);
                    resume.RelevanceCredentials = CheckNull(reader["relevance_credentials"]);
                    resume.HonourName = CheckNull(reader["honour_name"]);
                    resume.HonourUnits = CheckNull(reader["honour_units"]);
                    resume.HonourReason = CheckNull(reader["honour_reason"]);
                    resume.HonourLevel = CheckNull(reader["honour_level"]);
                    resume.HonourGetTime = CheckDatetimeNull(reader["honour_get_time"]);
                    resume.BaseInfoID = CheckIntNull(reader["person_id"]);
                    resume.type = CheckIntNull(reader["type"]);

                    list.Add(resume);
                }
            }
            return list;
        }

        public bool AddOrUpdateResume(Resume resume)
        {
            bool bNew = false;
            if (resume.ID == 0)
            {
                bNew = true;
            }

            string sql = string.Empty;
            MySqlParameter[] myParams;

            if (bNew)
            {
                sql = @"insert resume(education_school_name,education_school_character,education_specialty,education_dgree,
                               education_study_style,education_start_time,education_end_time,work_company,work_position,work_revenue,
                               work_departure_reason,work_prove_person_and_position,work_prove_person_contact,work_start_time,
                               work_end_time,project_name,project_intro,project_detail,project_start_time,project_end_time,relevance_credentials,
                               honour_name,honour_units,honour_reason,honour_level,honour_get_time,person_id,type) values(
                               ?education_school_name,?education_school_character,?education_specialty,?education_dgree,
                               ?education_study_style,?education_start_time,?education_end_time,?work_company,?work_position,?work_revenue,
                               ?work_departure_reason,?work_prove_person_and_position,?work_prove_person_contact,?work_start_time,
                               ?work_end_time,?project_name,?project_intro,?project_detail,?project_start_time,?project_end_time,?relevance_credentials,
                               ?honour_name,?honour_units,?honour_reason,?honour_level,?honour_get_time,?person_id,?type)";
                myParams = new MySqlParameter[]{
                new MySqlParameter("education_school_name",resume.EducationSchoolName),                              
                new MySqlParameter("education_school_character",resume.EducationSchoolCharacter),                       
                new MySqlParameter("education_specialty",resume.EducationSpecialty),                                      
                new MySqlParameter("education_dgree",resume.EducationdDgree),                                          
                new MySqlParameter("education_study_style",resume.EducationStudyStyle),      
                new MySqlParameter("education_start_time",resume.EducationStartTime),                                    
                new MySqlParameter("education_end_time",resume.EducationEndTime),                                    
                new MySqlParameter("work_company",resume.WorkCompany),                                              
                new MySqlParameter("work_position",resume.WorkPosition),                                                
                new MySqlParameter("work_revenue",resume.WorkRevenue),          
                new MySqlParameter("work_departure_reason",resume.WorkDepartureReason),                              
                new MySqlParameter("work_prove_person_and_position",resume.WorkProvePersonAndPosition),             
                new MySqlParameter("work_prove_person_contact",resume.WorkProvePersonContact),                       
                new MySqlParameter("work_start_time",resume.WorkStartTime),                                            
                new MySqlParameter("work_end_time",resume.WorkEndTime),    
                new MySqlParameter("project_name",resume.ProjectName),                                                
                new MySqlParameter("project_intro",resume.ProjectIntro),                                                  
                new MySqlParameter("project_detail",resume.ProjectDetail),                                                
                new MySqlParameter("project_start_time",resume.ProjectStartTime),                                         
                new MySqlParameter("project_end_time",resume.ProjectEndTime),         
                new MySqlParameter("relevance_credentials",resume.RelevanceCredentials),                                  
                new MySqlParameter("honour_name",resume.HonourName),                                                
                new MySqlParameter("honour_units",resume.HonourUnits),                                                  
                new MySqlParameter("honour_reason",resume.HonourReason),                                              
                new MySqlParameter("honour_level",resume.HonourLevel),        
                new MySqlParameter("honour_get_time",resume.HonourGetTime),                                           
                new MySqlParameter("person_id",resume.BaseInfoID),                                                 
                new MySqlParameter("type",resume.type)   
                };
            }
            else
            {
                sql = @"update resume set education_school_name=?education_school_name,education_school_character=?education_school_character,
                               education_specialty=?education_specialty,education_dgree=?education_dgree,education_study_style=?education_study_style,
                               education_start_time=?education_start_time,education_end_time=?education_end_time,work_company=?work_company,
                               work_position=?work_position,work_revenue=?work_revenue,work_departure_reason=?work_departure_reason,
                               work_prove_person_and_position=?work_prove_person_and_position,work_prove_person_contact=?work_prove_person_contact,
                               work_start_time=?work_start_time,work_end_time=?work_end_time,project_name=?project_name,project_intro=?project_intro,
                               project_detail=?project_detail,project_start_time=?project_start_time,project_end_time=?project_end_time,
                               relevance_credentials=?relevance_credentials,honour_name=?honour_name,honour_units=?honour_units,
                               honour_reason=?honour_reason,honour_level=?honour_level,honour_get_time=?honour_get_time,person_id=?person_id
                               where id=?id and type=?type";
                myParams = new MySqlParameter[]{
                     new MySqlParameter("education_school_name",resume.EducationSchoolName),                              
                     new MySqlParameter("education_school_character",resume.EducationSchoolCharacter),                       
                     new MySqlParameter("education_specialty",resume.EducationSpecialty),                                      
                     new MySqlParameter("education_dgree",resume.EducationdDgree),                                          
                     new MySqlParameter("education_study_style",resume.EducationStudyStyle),                                  
                     new MySqlParameter("education_start_time",resume.EducationStartTime),                                    
                     new MySqlParameter("education_end_time",resume.EducationEndTime),                                    
                     new MySqlParameter("work_company",resume.WorkCompany),                                              
                     new MySqlParameter("work_position",resume.WorkPosition),                                                
                     new MySqlParameter("work_revenue",resume.WorkRevenue),                                                
                     new MySqlParameter("work_departure_reason",resume.WorkDepartureReason),                              
                     new MySqlParameter("work_prove_person_and_position",resume.WorkProvePersonAndPosition),             
                     new MySqlParameter("work_prove_person_contact",resume.WorkProvePersonContact),                       
                     new MySqlParameter("work_start_time",resume.WorkStartTime),                                            
                     new MySqlParameter("work_end_time",resume.WorkEndTime),                                             
                     new MySqlParameter("project_name",resume.ProjectName),                                                
                     new MySqlParameter("project_intro",resume.ProjectIntro),                                                  
                     new MySqlParameter("project_detail",resume.ProjectDetail),                                                
                     new MySqlParameter("project_start_time",resume.ProjectStartTime),                                         
                     new MySqlParameter("project_end_time",resume.ProjectEndTime),                                          
                     new MySqlParameter("relevance_credentials",resume.RelevanceCredentials),                                  
                     new MySqlParameter("honour_name",resume.HonourName),                                                
                     new MySqlParameter("honour_units",resume.HonourUnits),                                                  
                     new MySqlParameter("honour_reason",resume.HonourReason),                                              
                     new MySqlParameter("honour_level",resume.HonourLevel),                                                  
                     new MySqlParameter("honour_get_time",resume.HonourGetTime),                                           
                     new MySqlParameter("person_id",resume.BaseInfoID),                                                 
                     new MySqlParameter("?id",resume.ID),
                     new MySqlParameter("type",resume.type)
                };
            }

            if (MySqlUtil.ExecuteNonQuery(sql, myParams) == -1)
            {
                return false;
            }

            resume.ID = bNew ? GetLastID("resume") : resume.ID;
            return true;
        }
    }
}

